package com.netdisk.service.impl;

import com.netdisk.dto.UserActivityDTO;
import com.netdisk.dto.request.file.AddFavoriteDTO;
import com.netdisk.dto.response.file.FavoriteDTO;
import com.netdisk.entity.Favorite;
import com.netdisk.entity.File;
import com.netdisk.exception.BusinessException;
import com.netdisk.mapper.FavoriteMapper;
import com.netdisk.mapper.FileMapper;
import com.netdisk.mapper.UserMapper;
import com.netdisk.service.FavoriteService;
import com.netdisk.service.UserActivityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Service
public class FavoriteServiceImpl implements FavoriteService {

    @Autowired
    private FileMapper fileMapper;
    @Autowired
    private FavoriteMapper favoriteMapper;
    @Autowired
    private UserActivityService userActivityService;
    @Autowired
    private UserMapper userMapper;
    /**
     * 将文件添加到收藏
     * @param addFavoriteDTO
     */
    public void addFavorite(AddFavoriteDTO addFavoriteDTO){
        Integer userId = addFavoriteDTO.getUserId();
        Integer fileId = addFavoriteDTO.getFileId();
        File file = fileMapper.getFileById(userId,fileId);

        if (!this.isFavorite(userId,fileId)) {
            file.setIsFavorite(true);
            favoriteMapper.insert(file);
            fileMapper.addFavorite(file);
            userActivityService.insertActivity(file,"添加文件到收藏夹","favorite");
        }
        else throw new BusinessException("文件已被收藏");
    }

    /**
     * 将文件从收藏中取消
     * @param userId
     * @param fileId
     */
    public void cancelFavorite(Integer userId, Integer fileId){
        File file = fileMapper.getFileById(userId,fileId);
        if (this.isFavorite(userId,fileId)){
            favoriteMapper.delete(file);
            file.setIsFavorite(false);
            fileMapper.cancelFavorite(file);
            userActivityService.insertActivity(file,"从收藏夹中移除文件","unfavorite");
        }
        else throw new BusinessException("该收藏不存在");
    }

    /**
     * 获取收藏文件数量
     * @param userId
     * @return
     */
    public Integer getFavoriteCountById(Integer userId){
        return favoriteMapper.getFavoriteCount();
    }

    /**
     * 获取收藏文件列表
     * @param userId
     * @return
     */
    public List<FavoriteDTO> getFavoriteListById(Integer userId){
        List<Favorite> favoriteList = favoriteMapper.getFavoriteListById(userId);
        List<FavoriteDTO> favoriteDTOList = new ArrayList<>();
        for (Favorite f : favoriteList){
            Integer fileId = f.getFileId();
            FavoriteDTO favoriteDTO = new FavoriteDTO();
            LocalDateTime createdAt = f.getCreatedAt();
            File file = fileMapper.getFileById(userId,fileId);
            favoriteDTO.setFile(file);
            favoriteDTO.setCreatedAt(createdAt);
            favoriteDTOList.add(favoriteDTO);
        }
        return favoriteDTOList;
    }


    /**
     * 检查文件是否已收藏
     * @param userId
     * @param fileId
     * @return
     */
    public boolean isFavorite(Integer userId, Integer fileId){
        File file = fileMapper.getFileById(userId,fileId);
        if (file != null) return file.getIsFavorite();
        else throw new BusinessException("文件不存在");
    }
}
